/* 
	验证元素
 */
function validateElement(el, body = true) {
	return el instanceof Element && (!body || (body && el.parentNode != document.documentElement));
}

/* 
	匹配最近的符合规则的父元素
 */
function findMeetParentElement(el, meet) {
	while(validateElement(el)) {
		if (meet(el)) break;
		el = el.parentNode;
	}
	return el;
}

/* 
	寻找滚动父节点
 */
function findNearestScrollElement(element, xy) {
	const property = xy ? `overflow-${xy}` : 'overflow';
	const regexp = /auto|scroll|overlay/;
	return findMeetParentElement(element, el => regexp.test(window.getComputedStyle(el, null).getPropertyValue(property)))
}

/* 
	获取canvas像素比
 */
function getCanvasRatio(target) {
	const ctx = target?.tagName == 'CANVAS' ? target.getContext('2d') : target;
	return (window.devicePixelRatio || 1) / (ctx.backingStorePixelRatio || 1);
}

export {
	validateElement,
	findMeetParentElement,
	findNearestScrollElement,
	getCanvasRatio
}